#!/usr/bin/env python

import gtk
import gtk.glade
import pango

import os

import uspCoreUtils 
import utils.GConfigurator as uspGConfig
import utils.ButtonFactory as uspButtonFactory

class SystemManagementView(object):
	def __init__(self, widgetTree, mainWindow, mainEventWindow):
		mainVBox = widgetTree.get_widget("MainVBox1")
		buttonFactory = uspButtonFactory.ButtonFactory()
		buttonFactory.iconSize = gtk.ICON_SIZE_BUTTON
		buttonFactory.minLabelWidth = 15
		buttonFactory.clearLabelAttributes()
		buttonFactory.addLabelAttribute(pango.AttrWeight(pango.WEIGHT_NORMAL, 0, -1))
		buttonFactory.addLabelAttribute(pango.AttrWeight(pango.WEIGHT_NORMAL, 0, -1))
		
		items = (
			('Install Software', 'synaptic', 'gksu synaptic'), 
			('Control Center','gnome-control-center', 'gnome-control-center'),
			('System monitor','utilities-system-monitor', 'gnome-system-monitor'),
			('Lock screen','system-lock-screen', 'gnome-screensaver-command --lock'),
			('Sleep monitor','screensaver', 'xset dpms force off'),
			('Quit...','gnome-logout', 'gnome-session-save --kill'))
		
		for item in items:
			label, icon, executable = item
			buttonFactory.addEvent("button_release_event", self.onButtonClicked, executable)
			button = buttonFactory.create([label], icon, label)
			mainVBox.pack_start(button, False, False, 0)
		
	def onButtonClicked(self, widget, event, executable):
		toExecList = executable.split()
		uspCoreUtils.execute(toExecList)

class Configuration(uspCoreUtils.Singleton):
	def __init__(self):
		uspCoreUtils.Singleton.__init__(self)
		
class UspPlugin(object):
	
	gladeFilename = "uspSystemManagement.glade"
	
	def __init__(self):
		self.gladefile = os.path.join(os.path.dirname(__file__) , self.gladeFilename)
		
		if not os.path.exists(self.gladefile):
			raise Exception("Glade file '"+ self.gladeFilename +"' does not exist.")
			
		self.widgetTree = gtk.glade.XML(self.gladefile, "MainWindow")
		self.window = self.widgetTree.get_widget("MainWindow")
		self.content_holder = self.widgetTree.get_widget("MainEventBox")
		
		self.gClient = uspGConfig.GConfigurator('/apps/usp2/plugins/system_management')
		Configuration().gClient = self.gClient
		
		self.userView = SystemManagementView(self.widgetTree, self.window, self.content_holder)
	
	def pluginName(self):
		return 'System management'
	
	def mainWidget(self):
		return self.content_holder
	
	def pluginIcon(self):
		return 'gnome-system'
